using Godot;
using Godot.Collections;
using System;

public partial class Sight : Area2D
{
	private CollisionPolygon2D _collisionPolygon2D;
	private Characters _characters;
	// Called when the node enters the scene tree for the first time.
	public void Initialize()
	{
		_collisionPolygon2D = GetNode<CollisionPolygon2D>("CollisionPolygon2D");
		_characters = Owner as Characters;
		_collisionPolygon2D.Polygon = SightArea();
		BodyEntered += OnSightBodyEntered;
	}

	// Called every frame. 'delta' is the elapsed time since the previous frame.
	public override void _Process(double delta)
	{
	}

	public Vector2[] SightArea()
	{
		Vector2[] area = new Vector2[4];
		var sightlen = _characters.CharacterData.sight;
		var y = sightlen / 2 * 1.732;
		var x = sightlen / 2 ;
		area[0] = Vector2.Zero;
		area[1] = new Vector2(x,(int)y);
		area[2] = new Vector2(sightlen,0);
		area[3] = new Vector2(x,-1*(int)y);
		return area;
	}

	public void OnSightBodyEntered(Node2D body)
	{
		if (body is Characters)
		{//GD.Print("进入视野",body.Name);
		}
	}
}
